import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../utils/app_theme.dart';
import 'dashboard_screen.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final _loginFormKey = GlobalKey<FormState>();
  final _signupFormKey = GlobalKey<FormState>();
  
  final _loginEmailController = TextEditingController();
  final _loginPasswordController = TextEditingController();
  
  final _signupNameController = TextEditingController();
  final _signupEmailController = TextEditingController();
  final _signupPasswordController = TextEditingController();
  final _signupConfirmPasswordController = TextEditingController();
  
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
  }
  
  @override
  void dispose() {
    _tabController.dispose();
    _loginEmailController.dispose();
    _loginPasswordController.dispose();
    _signupNameController.dispose();
    _signupEmailController.dispose();
    _signupPasswordController.dispose();
    _signupConfirmPasswordController.dispose();
    super.dispose();
  }
  
  void _login() {
    if (_loginFormKey.currentState!.validate()) {
      // In a real app, implement authentication logic here
      // For now, just navigate to dashboard
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (context) => const DashboardScreen()),
      );
    }
  }
  
  void _signup() {
    if (_signupFormKey.currentState!.validate()) {
      // In a real app, implement registration logic here
      // For now, just navigate to dashboard
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (context) => const DashboardScreen()),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.backgroundColor,
      body: SafeArea(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: [
              const SizedBox(height: 20),
              
              // App title and slogan
              Text(
                '原子习惯',
                style: AppTheme.headingStyle.copyWith(
                  color: AppTheme.primaryColor,
                  fontSize: 32,
                ),
              ),
              const SizedBox(height: 8),
              const Text(
                '基于科学方法养成好习惯',
                style: AppTheme.bodyStyle,
              ),
              const SizedBox(height: 40),
              
              // Login/Signup card
              Container(
                decoration: AppTheme.cardDecoration,
                child: Column(
                  children: [
                    // Tabs
                    TabBar(
                      controller: _tabController,
                      tabs: const [
                        Tab(text: '登录'),
                        Tab(text: '注册'),
                      ],
                    ),
                    
                    // Tab contents
                    SizedBox(
                      height: 340,
                      child: TabBarView(
                        controller: _tabController,
                        children: [
                          // Login tab
                          Padding(
                            padding: const EdgeInsets.all(16.0),
                            child: Form(
                              key: _loginFormKey,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  TextFormField(
                                    controller: _loginEmailController,
                                    decoration: AppTheme.inputDecoration(
                                      '电子邮箱',
                                      hintText: 'your@email.com',
                                      prefixIcon: const Icon(Icons.email),
                                    ),
                                    keyboardType: TextInputType.emailAddress,
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请输入电子邮箱';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 16),
                                  TextFormField(
                                    controller: _loginPasswordController,
                                    decoration: AppTheme.inputDecoration(
                                      '密码',
                                      hintText: '******',
                                      prefixIcon: const Icon(Icons.lock),
                                    ),
                                    obscureText: true,
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请输入密码';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 16),
                                  Align(
                                    alignment: Alignment.centerRight,
                                    child: TextButton(
                                      onPressed: () {
                                        // Implement forgot password
                                      },
                                      child: const Text('忘记密码？'),
                                    ),
                                  ),
                                  const SizedBox(height: 16),
                                  ElevatedButton(
                                    onPressed: _login,
                                    style: AppTheme.primaryButtonStyle,
                                    child: const Text('登录'),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          
                          // Signup tab
                          Padding(
                            padding: const EdgeInsets.all(16.0),
                            child: Form(
                              key: _signupFormKey,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  TextFormField(
                                    controller: _signupNameController,
                                    decoration: AppTheme.inputDecoration(
                                      '姓名',
                                      hintText: '请输入姓名',
                                      prefixIcon: const Icon(Icons.person),
                                    ),
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请输入姓名';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 16),
                                  TextFormField(
                                    controller: _signupEmailController,
                                    decoration: AppTheme.inputDecoration(
                                      '电子邮箱',
                                      hintText: 'your@email.com',
                                      prefixIcon: const Icon(Icons.email),
                                    ),
                                    keyboardType: TextInputType.emailAddress,
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请输入电子邮箱';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 16),
                                  TextFormField(
                                    controller: _signupPasswordController,
                                    decoration: AppTheme.inputDecoration(
                                      '密码',
                                      hintText: '******',
                                      prefixIcon: const Icon(Icons.lock),
                                    ),
                                    obscureText: true,
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请输入密码';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 16),
                                  TextFormField(
                                    controller: _signupConfirmPasswordController,
                                    decoration: AppTheme.inputDecoration(
                                      '确认密码',
                                      hintText: '******',
                                      prefixIcon: const Icon(Icons.lock),
                                    ),
                                    obscureText: true,
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return '请确认密码';
                                      }
                                      if (value != _signupPasswordController.text) {
                                        return '两次密码不一致';
                                      }
                                      return null;
                                    },
                                  ),
                                  const SizedBox(height: 24),
                                  ElevatedButton(
                                    onPressed: _signup,
                                    style: AppTheme.primaryButtonStyle,
                                    child: const Text('注册'),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              
              const SizedBox(height: 24),
              
              // Social login options
              const Text(
                '或通过以下方式登录',
                style: AppTheme.smallTextStyle,
              ),
              const SizedBox(height: 16),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  OutlinedButton.icon(
                    onPressed: () {
                      // Implement Apple login
                    },
                    icon: const FaIcon(FontAwesomeIcons.apple),
                    label: const Text('Apple'),
                    style: AppTheme.outlineButtonStyle.copyWith(
                      minimumSize: MaterialStateProperty.all(const Size(120, 50)),
                    ),
                  ),
                  const SizedBox(width: 16),
                  OutlinedButton.icon(
                    onPressed: () {
                      // Implement WeChat login
                    },
                    icon: const FaIcon(FontAwesomeIcons.weixin),
                    label: const Text('微信'),
                    style: AppTheme.outlineButtonStyle.copyWith(
                      minimumSize: MaterialStateProperty.all(const Size(120, 50)),
                    ),
                  ),
                ],
              ),
              
              const SizedBox(height: 24),
              
              // Terms and privacy
              Text(
                '注册或登录代表您同意我们的服务条款和隐私政策',
                style: AppTheme.smallTextStyle.copyWith(fontSize: 12),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
